//
//  DDLoginViewController.m
//  Dingdong
//
//  Created by 程强 on 16/11/14.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "DDLoginViewController.h"
#import "DDRegisterViewController.h"
#import "DDRetrievePwdViewController.h"
#import "DDLoginManager.h"
#import "DDCartManager.h"
#import "DDTabBarController.h"

@interface DDLoginViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollHeight;
@property (weak, nonatomic) IBOutlet UIView *loginBackView;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet UITextField *accountTxf;
@property (weak, nonatomic) IBOutlet UITextField *pwdTxf;
@property (weak, nonatomic) IBOutlet UIImageView *accountMark;
@property (weak, nonatomic) IBOutlet UIImageView *pwdMark;
@property (weak, nonatomic) IBOutlet UILabel *companyNameLbl;
@property (strong, nonatomic) DDLoginManager *manager;
@property (strong, nonatomic) DDCartManager *cartManager;
@end

@implementation DDLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.manager = [DDLoginManager new];
    self.cartManager = [DDCartManager new];
    [self layoutLoginView];
}
- (void)layoutLoginView
{
    [self.accountTxf becomeFirstResponder];
    self.scrollWidth.constant = ScreenWidth;
    self.scrollHeight.constant = ScreenHeight-64;
    self.loginBackView.layer.cornerRadius = 3.0;
    self.loginBtn.layer.cornerRadius = 3.0;
    self.companyNameLbl.adjustsFontSizeToFitWidth = YES;
}
- (void)setLeftBarItem
{
    UIImage *image = [LoadImage(@"login_close")imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(gotoBack)];
    self.navigationItem.leftBarButtonItem = item;
}
- (void)gotoBack
{
    [self resignKeyboard];
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)userLoginSuccessCallBack:(LoginSuccessCallBack)block
{
    self.successBlock = block;
}
#pragma mark --- 获取个人信息
- (void)fetchUserInfo
{
    [[self.manager getUserInfo]subscribeNext:^(id x) {
        
    } error:^(NSError *error) {
        
    } completed:^{
        
    }];
}
- (void)fetchCartNumber
{
    [[SystemConfig shareInstance]updateCartCountSueesss:^(NSString *count) {
        
    }];
}
#pragma mark ---IBAction
- (IBAction)loginClick:(id)sender
{
    if (self.accountTxf.text.length == 0||self.pwdTxf.text.length == 0) {
        return;
    }
    [self resignKeyboard];
    weak_Self(self);
    [self showMessageHUD:@"Sign in ..."];
    [[self.manager loginIn:self.accountTxf.text password:self.pwdTxf.text]subscribeNext:^(id x) {
        [SystemConfig shareInstance].token = x[@"data"];
        [SystemConfig shareInstance].isLogin = YES;
        [weakSelf gotoBack];
        if (weakSelf.successBlock) {
            weakSelf.successBlock();
        }
        [weakSelf fetchUserInfo];
        [weakSelf fetchCartNumber];
    } error:^(NSError *error) {
        
    } completed:^{
        [weakSelf hideHUD];
    }];
}
- (IBAction)showPwdClick:(id)sender
{
    UIButton *btn = (UIButton *)sender;
    BOOL show = self.pwdTxf.secureTextEntry;
    btn.selected = show;
    self.pwdTxf.secureTextEntry = !self.pwdTxf.secureTextEntry;
}
- (IBAction)signUpClick:(id)sender
{
    DDRegisterViewController *vc = StoryBoardViewController(@"Login", @"DDRegisterViewController");
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)forgetPasswordClick:(id)sender
{
    DDRetrievePwdViewController *vc = StoryBoardViewController(@"Login", @"DDRetrievePwdViewController");
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark --- UITextFieldDelegate
- (void)resignKeyboard
{
    [self.accountTxf resignFirstResponder];
    [self.pwdTxf resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField == self.accountTxf) {
        self.accountMark.highlighted = YES;
        self.pwdMark.highlighted = NO;
    }else{
        self.accountMark.highlighted = NO;
        self.pwdMark.highlighted = YES;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
